package com.china08.yxyapi.api.manage.tec.teaching.homework.myques;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkQuesGroupRelQues;
import com.china08.yxyapi.entity.mg.res.YxyResQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyResQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRelQuesRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.service.mg.HomeworkQuesGroupService;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class HomeworkMyQuesDelControllerextends extends YxyApiBaseController {

    @Autowired
    private HomeworkQuesGroupRelQuesRepository homeworkQuesGroupRelQuesRepository;

    @Autowired
    private YxyResQuesRepository yxyResQuesRepository;

    @Autowired
    private ElasticQuesService elasticQuesService;

    @Autowired
    private HomeworkQuesGroupService homeworkQuesGroupService;

    @ApiOperation(value = "删除试题")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/myques/ques/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        YxyProfile profile = getProfile(request, response);
        String teacherId = profile.getId();
        String schoolId = profile.getSchoolId();
        List<String> quesIds = reqModel.quesId;
        for (String string : quesIds) {
            ElasticYxyYxyQuesResource elasticYxyYxyQuesResource = elasticQuesService.findByEsQuesId(string);
            if (elasticYxyYxyQuesResource == null) {
                continue;
            }
            if (StringUtils.equals(elasticYxyYxyQuesResource.getCreatedBy(), teacherId)) {
                // 自己的试题
                // 自己的试题得删除大题库的
                YxyResQues ques = yxyResQuesRepository.findById(string);
                ques.setDeleteFlag("1");
                ques.setSycEsFlag(0);
                yxyResQuesRepository.save(ques);
            }
            HomeworkQuesGroupRelQues groupRelQues = homeworkQuesGroupRelQuesRepository
                    .findByQuesIdAndTeacherIdAndSchoolIdAndGroupId(string, teacherId, schoolId, reqModel.groupId);
            if (groupRelQues != null) {
                // 删除试题的引用
                homeworkQuesGroupRelQuesRepository.delete(groupRelQues);
                homeworkQuesGroupService.updateQuesCount(reqModel.groupId);
            }
        }
        return new YxyApiRespModel("删除成功");
    }

    protected static class ReqModel extends YxyApiReqModel {

        public List<String> quesId;

        public String groupId;

        @Override
        public void autoCheck() {
            Assert.isTrue(quesId != null && quesId.size() > 0, "请选择要删除的试题");
            Assert.notNull(StringUtils.defaultIfBlank(groupId, null), "请填写要删除试题的组");
        }
    }

}
